﻿#include "DAFuncTools.h"
#include "DATray.h"
#include <QDebug>
#include <QApplication>
#include <QProcess>
#if defined(Q_OS_WIN)
#include <windows.h>
#endif
DATray::DATray(QWidget *parent)
    : QSystemTrayIcon(parent),
    Pobj(parent)
{
    setDialog = new DAConfigDialog();
    setIcon(QIcon(QPixmap(":/Style/image/da4.svg")));
    setToolTip(tr("桌面助手"));
    connect(this, &DATray::activated, this, &DATray::onIconActivated);

    trayMenu = new QMenu();
    QAction *Setting = new QAction(QIcon(QPixmap(":/Style/image/setting2.svg")),"设置",trayMenu);
    QAction *HideMainWdiget = new QAction(QIcon(QPixmap(":/Style/image/hide1.svg")),"隐藏",trayMenu);
    QAction *OpenSysMon = new QAction(QIcon(QPixmap(":/Style/image/monitoring1.svg")),"监视器",trayMenu);

#if defined(Q_OS_LINUX)
    QAction *Refresh = new QAction(QIcon(QPixmap(":/Style/image/refresh.svg")),"重启dde-dock",trayMenu);
#endif

    QAction *SplitLine=new QAction(trayMenu);
    SplitLine->setSeparator(true);
    QAction *ReStartApp = new QAction(QIcon(QPixmap(":/Style/image/refresh.svg")),"重启App",trayMenu);
    QAction *QuitApp = new QAction(QIcon(QPixmap(":/Style/image/close.svg")),"退出",trayMenu);
    trayMenu->addAction(Setting);
    trayMenu->addAction(HideMainWdiget);
    trayMenu->addAction(OpenSysMon);
#if defined(Q_OS_LINUX)
    trayMenu->addAction(Refresh);
#endif

    trayMenu->addAction(SplitLine);
    trayMenu->addAction(ReStartApp);
    trayMenu->addAction(QuitApp);
    setContextMenu(trayMenu);

    connect(Setting, &QAction::triggered, [=](bool) {
        setDialog->exec();
    });
    connect(HideMainWdiget, &QAction::triggered, [=](bool) {
        if(Pobj->isHidden())
        {
            Pobj->show();
            HideMainWdiget->setText(QString("隐藏"));
            HideMainWdiget->setIcon(QIcon(QPixmap(":/Style/image/hide1.svg")));
        }
        else
        {
            Pobj->close();
            HideMainWdiget->setText(QString("显示"));
            HideMainWdiget->setIcon(QIcon(QPixmap(":/Style/image/show1.svg")));
        }
    });
    connect(OpenSysMon, &QAction::triggered, [=](bool) {

#if defined(Q_OS_WIN)
        QProcess::startDetached("C:\\Windows\\System32\\Taskmgr.exe",QStringList());
#elif defined(Q_OS_LINUX)
        QProcess::startDetached("deepin-system-monitor",QStringList());
#endif
    });

#if defined(Q_OS_LINUX)
    connect(Refresh, &QAction::triggered, [=](bool) {
        QProcess::startDetached("pkill",{"dde-dock"});
    });
#endif

    connect(ReStartApp, &QAction::triggered, [=](bool) {
        qApp->exit();
        DAFunTools::GetInst()->isRestart = true;
    });
    connect(QuitApp, &QAction::triggered, [=](bool) {
        std::exit(0);
        DAFunTools::GetInst()->isRestart = false;
    });
    connect(this,&DATray::ShowMain,[=]() {
        HideMainWdiget->setText(QString("隐藏"));
        HideMainWdiget->setIcon(QIcon(QPixmap(":/Style/image/hide1.svg")));
    });
}

void DATray::onIconActivated(QSystemTrayIcon::ActivationReason reason) {
    if (reason == QSystemTrayIcon::Trigger)
    {
#if 0
        std::exit(0);
        DAFunTools::GetInst()->isRestart = false;
#else
        if(Pobj->isHidden())
        {
            Pobj->show();
            emit ShowMain();
        }
        else
        {
            Pobj->close();
        }
#endif
    }
}

bool DATray::event(QEvent * event)
{
    if (QEvent::ActivationChange == event->type()) {
        if (trayMenu->isVisible()){
            trayMenu->close();
        }
    }
    return QSystemTrayIcon::event(event);
}
